package com.mofang.utils;

import java.io.Serializable;

public class LoanCalculate implements Serializable {

	private static final long serialVersionUID = -556167158001639680L;


	/*** 公积金贷款 */
	public final static int Gjj_loan = 1;
	/*** 商业贷款 */
	public final static int Sy_loan = 2;
	/*** 混合贷款 */
	public final static int Group_loan = 3;

	/*** 等额本金 */
	public final static int Repay_debj = 4;
	/*** 等额本息 */
	public final static int Repay_debx = 5;

	/*** 贷款模式*/
	private int lendingModel;
	/*** 还款模式 */
	private int repayment;
	/*** 公积金贷款总额 */
	private int lc_gjjtotal;
	/*** 商业贷款总额 */
	private int lc_syjtotal;
	/*** 公积金贷款利率 单位整数*/
	/*** 公积金贷款利率 */
	private float lc_gjjProfit;
	/*** 商业贷款利率 */
	private float lc_syjProfit;
	/*** 按揭年数 */
	private int lc_yearLongth;

	private float payProfit;
	private float payPermonth;
	
	
	public LoanCalculate calculate(){
		float year=lc_yearLongth;
			if (lendingModel==Gjj_loan) {
				float benjin=lc_gjjtotal*10000;
				float nianlilu=lc_gjjProfit/100;
				payPermonth=calculateDebxDebs(nianlilu, benjin, year);
				payProfit=payPermonth*year*12-benjin;
			}
			if (lendingModel==Sy_loan) {
				float benjin=lc_syjtotal*10000;
				float nianlilu=lc_syjProfit/100;
				payPermonth=calculateDebxDebs(nianlilu, benjin, year);
				payProfit=payPermonth*year*12-benjin;
			}
			if (lendingModel==Group_loan) {
				float benjin1=lc_gjjtotal*10000;
				float nianlilu1=lc_gjjProfit/100;
				payPermonth=calculateDebxDebs(nianlilu1, benjin1, year);
				payProfit=payPermonth*year*12-benjin1;
				
				float benjin2=lc_syjtotal*10000;
				float nianlilu2=lc_syjProfit/100;
				payPermonth+=calculateDebxDebs(nianlilu2, benjin2, year);
				payProfit+=payPermonth*year*12-benjin2;
			}
			return this;

	}
	private float calculateDebxDebs(float nianlilu,float benjin,float year){
		float yuelilu=nianlilu/12;
		float month= year*12;
		if (repayment==Repay_debj){
			// 等额本金 A*x(1+1/x)/2*p
			float sumpro =benjin*month*(1+1/month)/2*yuelilu;
			float perav=(sumpro+benjin)/month;
			return perav;
		}
		else {
			// 等额本息 每月月供额=〔贷款本金×月利率×(1＋月利率)＾还款月数〕÷〔(1＋月利率)＾还款月数-1〕
			if (yuelilu==0) {
				return benjin/month;
			}
			float perav=(float) ((benjin*yuelilu)*Math.pow((1+yuelilu), month)/(Math.pow((1+yuelilu), month)-1));
			return perav;
		}
	}

	
	public int getLendingModel() {
		return lendingModel;
	}

	public void setLendingModel(int lendingModel) {
		this.lendingModel = lendingModel;
	}

	public int getRepayment() {
		return repayment;
	}

	public void setRepayment(int repayment) {
		this.repayment = repayment;
	}

	public int getLc_gjjtotal() {
		return lc_gjjtotal;
	}

	public void setLc_gjjtotal(int lc_gjjtotal) {
		this.lc_gjjtotal = lc_gjjtotal;
	}

	public int getLc_syjtotal() {
		return lc_syjtotal;
	}

	public void setLc_syjtotal(int lc_syjtotal) {
		this.lc_syjtotal = lc_syjtotal;
	}

	public float getLc_gjjProfit() {
		return lc_gjjProfit;
	}

	public void setLc_gjjProfit(float lc_gjjProfit) {
		this.lc_gjjProfit = lc_gjjProfit;
	}

	public float getLc_syjProfit() {
		return lc_syjProfit;
	}

	public void setLc_syjProfit(float lc_syjProfit) {
		this.lc_syjProfit = lc_syjProfit;
	}

	public int getLc_yearLongth() {
		return lc_yearLongth;
	}

	public void setLc_yearLongth(int lc_yearLongth) {
		this.lc_yearLongth = lc_yearLongth;
	}

	public float getPayProfit() {
		return payProfit;
	}

	public void setPayProfit(float payProfit) {
		this.payProfit = payProfit;
	}

	public float getPayPermonth() {
		return payPermonth;
	}

	public void setPayPermonth(float payPermonth) {
		this.payPermonth = payPermonth;
	}
}